package day6;


import java.util.Stack;

class ListNode {
    int val;
    ListNode next = null;
    public ListNode(int val) {
      this.val = val;
    }
  }


public class demo1 {
    public static void main(String[] args) {
    }

    public ListNode addInList(ListNode head1, ListNode head2) {
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        ListNode cur1 = head1;
        ListNode cur2 = head2;
        while (cur1 != null) {
            stack1.add(cur1.val);
            cur1 = cur1.next;
        }
        while (cur2 != null) {
            stack2.add(cur2.val);
            cur2 = cur2.next;
        }
        int k = 0;
        ListNode t = null;
        while (!stack1.isEmpty() && !stack2.isEmpty()) {
            int x1 = stack1.pop();
            int x2 = stack2.pop();
            ListNode node = new ListNode((x1 + x2 + k) % 10);
            node.next = t;
            t = node;
            if (x1 + x2 + k >= 10) k = 1;
        }
        while (!stack1.isEmpty()){
            int x1=stack1.pop();
            ListNode node = new ListNode((x1 + k) % 10);
            node.next = t;
            t = node;
            if (x1 + k >= 10) k = 1;
        }

        while (!stack2.isEmpty()){
            int x1=stack2.pop();
            ListNode node = new ListNode((x1 + k) % 10);
            node.next = t;
            t = node;
            if (x1 + k >= 10) k = 1;
        }

        if (k != 0) {
            ListNode node = new ListNode(1);
            node.next=t;
            t=node;
        }
     return t;
    }



}
